home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / NCSA⁄BYU TCP⁄IP / macutil.c < prev    next >
Text File  |  1991-12-17  |  11KB  |  552 lines

  1. /*  MACUTIL.C
  2. *   Utilities for the network library that are Macintosh specific
  3. ****************************************************************************
  4. *                                                                          *
  5. *      part of:                                                            *
  6. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  7. *      by Tim Krauskopf                                                    *
  8. *                                                                          *
  9. *      National Center for Supercomputing Applications                     *
  10. *      152 Computing Applications Building                                 *
  11. *      605 E. Springfield Ave.                                             *
  12. *      Champaign, IL  61820                                                *
  13. *                                                                          *
  14. *                                                                          *
  15. ****************************************************************************
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #ifdef UNDERNOCIRCUMSTANCES
  21. #include "protocol.h"
  22. #include "data.h"
  23. #endif
  24.  
  25. #ifdef MPW
  26. #include "mpw.h"
  27. #endif
  28.  
  29. #include <Dialogs.h>
  30. #include <Files.h>
  31. #include <Types.h>
  32. #include <AppleTalk.h>
  33. #include <Events.h>
  34. #include <Errors.h>
  35. #include <OSutils.h>
  36. #include <Memory.h>
  37. #include <Strings.h>
  38.  
  39. #include "configrec.h"
  40. #include "maclook.h"
  41.  
  42. /* Some globals for file lookup */
  43. int defaultv;
  44.  
  45. ParamBlockRec FLpb;
  46. CInfoPBRec HFLpb, tHFLpb;
  47. char FileName[256], FileTemplate[256];
  48. extern int * DirTree;
  49. char *iobufptr=0L,*iobuf=0L,*iobufmax=0L;
  50. int iobufsize,lastcr;
  51.  
  52. int getWDnum()
  53.   /* returns reference of current default volume or working directory */
  54.   {
  55.     ParamBlockRec pb;
  56.  
  57.     pb.ioParam.ioNamePtr = 0L;
  58.     PBGetVol(&pb, FALSE);    /* see Tech Note 140 for implications of this */
  59.     return(pb.ioParam.ioVRefNum);
  60.   }
  61.  
  62. Str255 *getVname()
  63. {
  64.     WDPBRec pb;
  65.     char *temp;
  66.  
  67.     temp=NewPtr(256);
  68.  
  69.     pb.ioNamePtr=temp;
  70.     PBGetVol((ParmBlkPtr) &pb,FALSE);
  71.     (*temp)++;
  72.     temp[*temp]=':';
  73.     temp[*temp+1]=0;
  74.     return        /* This proc returns the Vol reference */
  75.         (Str255 *) temp;
  76. }
  77.  
  78. Str255 *getWDname()
  79. {
  80.     WDPBRec pb;
  81.     CInfoPBRec cpb;
  82.     char tempst[100],*wdtemp,*wdout,*start,*store,*trav;
  83.     int i,j;
  84.  
  85.     if (!isHFS()) return(getVname()); /* if not HFS then only volume exists */
  86.  
  87.     wdout=NewPtr(256);
  88.     wdtemp=NewPtr(256);
  89.  
  90.     sprintf(tempst,"ioNamePtr = %lx",wdout);
  91.     putln (tempst);
  92.  
  93.     pb.ioNamePtr=wdout;
  94.     PBHGetVol(&pb,FALSE);
  95.  
  96. #ifdef WANNACOLON
  97.     (*wdout)++;
  98.     wdout[*wdout]=':';
  99. #endif WANNACOLON
  100.  
  101.     cpb.dirInfo.ioCompletion=0L;
  102.     cpb.dirInfo.ioVRefNum=pb.ioWDVRefNum;
  103.     cpb.dirInfo.ioDrDirID=pb.ioWDDirID;
  104.     trav=wdtemp;
  105.     DirTree[ (j=0) ]=pb.ioVRefNum;
  106.  
  107.     while(cpb.dirInfo.ioDrDirID!=0) {
  108.         cpb.hFileInfo.ioNamePtr=trav;
  109.         cpb.hFileInfo.ioFDirIndex=-1;
  110.         if (PBGetCatInfo(&cpb,FALSE)!=0)  {cpb.dirInfo.ioDrDirID=0; break;}
  111.         if (*trav==0) {cpb.dirInfo.ioDrDirID=0;break;}
  112.         i=*trav; *trav=0; start=trav+1;
  113.         trav+=i+1; *trav=0;
  114.         DirTree[j++]=cpb.dirInfo.ioDrDirID;
  115.         cpb.dirInfo.ioDrDirID=cpb.dirInfo.ioDrParID;
  116.         }
  117.  
  118.     *wdtemp=0;
  119.     store=wdout+*wdout+1;
  120.     if (trav-wdtemp) *wdout=(trav-wdtemp);
  121.     if (trav!=wdtemp) start=trav-1; else start=wdtemp;
  122.  
  123.     if (start!=wdtemp) {
  124.         while(*start) start--;        /* Go to beginning of string */
  125.         if (start!=wdtemp) start--;
  126.         }
  127.  
  128.     while(start!=wdtemp) {
  129.         while(*start) start--;        /* Go to beginning of string */
  130.         trav=start+1; *store='/';    /* Ready to move directory name */
  131.         store++;
  132.         while (*trav) {*store=*trav; store++;trav++; } /* store it */
  133.         if (start!=wdtemp) start --;
  134.         *store=0;
  135.         }
  136.     DisposPtr(wdtemp);
  137.     return((Str255 *) wdout);
  138. }
  139.  
  140. setWDnum( num)
  141. int num;
  142. {
  143.     WDPBRec pb;
  144.  
  145.     pb.ioNamePtr=0L; 
  146.     pb.ioVRefNum=num;
  147.     pb.ioWDDirID=0L;
  148.     PBHSetVol(&pb,FALSE);
  149. }
  150.  
  151. /*
  152. *  setSFdir
  153. *  trick to set up the working directory from the last SFgetfile. TK
  154. */
  155.  
  156. #define SFSaveDisk  0x214
  157. #define CurDirStore 0x398
  158.  
  159. void setSFdir
  160.   (
  161.     void
  162.   )
  163. {
  164.     WDPBRec pb;
  165.  
  166.     if (!isHFS()) {
  167.         defaultv = - (* (short *) SFSaveDisk);
  168.         return;
  169.     }
  170.     
  171.     pb.ioNamePtr=0L; 
  172.     pb.ioWDProcID=0L; 
  173.     pb.ioVRefNum=  - (* (short *) SFSaveDisk);        /* saved from last SF package call */
  174.     pb.ioWDDirID= (* (int *) CurDirStore);
  175.     PBOpenWD(&pb,FALSE);
  176.     
  177.     defaultv = pb.ioVRefNum;
  178.     setvol(NULL, defaultv);            /* make working directory */
  179.  
  180. }
  181.  
  182. setWDname( num, name)
  183. int num;
  184. Str255 *name;
  185. {
  186. #pragma unused(name)
  187.     WDPBRec pb;
  188.  
  189.     pb.ioNamePtr=0L; 
  190.     pb.ioVRefNum=num;
  191.     pb.ioWDDirID=0L;
  192.     PBHSetVol(&pb,FALSE);
  193. }
  194.  
  195. int wccheck(file,template)    /* BYU - routine now returns (int) for recursion. */
  196. char *template,*file;
  197. {
  198.     while(*template) {
  199.         if (*template=='*') {
  200.             template++;
  201.             if (*template) {
  202.                 while((*file) && !wccheck(file,template)) file++;    /* BYU */
  203.                 if ((*file)==0) return(0);
  204.                 }
  205.             else return(1);
  206.             continue;
  207.             }
  208.         else
  209.             if ((*template!='?') && (*template!=*file)) return(0);
  210.         template++;file++;
  211.         }
  212.     if (*file)            /* BYU */
  213.         return(0);        /* BYU */
  214.     else                /* BYU */
  215.         return(1);        /* BYU */
  216. }
  217.  
  218. /**********************************************************************/
  219. /*
  220. *   Find directory name -- return a code that indicates whether the
  221. *   directory exists or not.
  222. *   0 = dir name ok
  223. *   -1 = error
  224. *   > 0 = dos error code, no dir by this name
  225. *
  226. *   Accept certain unix conventions, like '/' for separator
  227. *
  228. *   Also, append a '\' to the name before returning
  229. *  Note:  There must be enough room in the string to append the '\'
  230. */
  231.  
  232. direxist(dirname)
  233.     char dirname[];
  234.     {
  235. #pragma unused(dirname)
  236.     return(-1);
  237.     }
  238.  
  239. /**********************************************************************/
  240. /* firstname
  241. *  find the first name in the given directory which matches the wildcard
  242. *  specification
  243. *
  244. *  must NewPtr enough space for the path plus a full filename
  245. *
  246. *  expand '*' (unix) to '*.*' (dos)
  247. */
  248. char *firstname(spec)
  249. char *spec;
  250. {
  251.     char *loader;
  252.     int HFS;
  253.  
  254.     setvol(0L, defaultv);                            /* Go to default Directory */
  255.  
  256.     if ( (HFS=isHFS()) )
  257.         PBHGetVol((WDPBPtr) &HFLpb, FALSE);
  258.     else
  259.          PBGetVol(&FLpb,FALSE);
  260.  
  261.     loader=FileTemplate;
  262.     while(*loader++=*spec++);
  263.     putln(spec);
  264.     putln(FileTemplate);
  265.     if ( HFS) {
  266.         HFLpb.hFileInfo.ioCompletion=0;
  267.         HFLpb.hFileInfo.ioNamePtr=FileName;
  268.         HFLpb.hFileInfo.ioFVersNum=0;
  269.         HFLpb.hFileInfo.ioFDirIndex=1;
  270.         tHFLpb=HFLpb;
  271.         if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  272.         }
  273.     else {
  274.         FLpb.fileParam.ioCompletion=0;
  275.         FLpb.fileParam.ioNamePtr=FileName;
  276.         FLpb.fileParam.ioFVersNum=0;
  277.         FLpb.fileParam.ioFDirIndex=1;
  278.         if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  279.         }
  280.     FileName[FileName[0]+1]=0;
  281.     while(!wccheck(&FileName[1],FileTemplate)) {
  282.         if (HFS) {
  283.             tHFLpb.hFileInfo.ioFDirIndex++;
  284.             HFLpb=tHFLpb;
  285.             if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  286.             }
  287.         else {
  288.             FLpb.fileParam.ioFDirIndex++;
  289.             if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  290.             }
  291.         FileName[FileName[0]+1]=0;
  292.         }
  293.  
  294.     if (isHFS() && (HFLpb.hFileInfo.ioFlAttrib & 16))  {
  295.         FileName[++FileName[0]]='/';
  296.         FileName[FileName[0]+1]=0;
  297.         }
  298.  
  299.     return(&FileName[1]);
  300. }
  301.  
  302. /**********************************************************************/
  303. /* nextname
  304. *  modify the path spec to contain the next file name in the
  305. *  sequence as given by DOS
  306. *
  307. *  if at the end of the sequence, return NULL
  308. */
  309. char *nextname()
  310. {
  311.     if (isHFS() ) {
  312.         tHFLpb.hFileInfo.ioFDirIndex++;
  313.         HFLpb=tHFLpb;
  314.         if (HFLpb.hFileInfo.ioNamePtr!=FileName) return(0L);
  315.         if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  316.         FileName[FileName[0]+1]=0;
  317.         while(!wccheck(&FileName[1],FileTemplate)) {
  318.             putln(&FileName[1]);
  319.             tHFLpb.hFileInfo.ioFDirIndex++;
  320.             HFLpb=tHFLpb;
  321.             if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  322.             FileName[FileName[0]+1]=0;
  323.             }
  324.         }
  325.     else {
  326.         if (FLpb.fileParam.ioNamePtr!=FileName) return(0L);
  327.         FLpb.fileParam.ioFDirIndex++;
  328.         if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  329.         FileName[FileName[0]+1]=0;
  330.         while(!wccheck(&FileName[1],FileTemplate)) {
  331.             FLpb.fileParam.ioFDirIndex++;
  332.             if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  333.             FileName[FileName[0]+1]=0;
  334.             }
  335.         }
  336.  
  337.     if (isHFS() && (HFLpb.hFileInfo.ioFlAttrib & 16))  {
  338.         FileName[++FileName[0]]='/';
  339.         FileName[FileName[0]+1]=0;
  340.         }
  341.  
  342.     return(&FileName[1]);
  343. }
  344.  
  345.  
  346. /************************************************************************/
  347. /* userabort
  348. *  check to see if the user wants to abort
  349. *  For the PC, just see if the user has pressed ESC
  350. *  return true if the user wants to abort
  351. */
  352. int userabort
  353.   (
  354.     void
  355.   )
  356. {
  357.     EventRecord theEvent;
  358.  
  359.     while (GetNextEvent(24,&theEvent)) {
  360.         if ((theEvent.modifiers & cmdKey) &&
  361.             (theEvent.message & 0xff) =='.') 
  362.             return(-1);
  363.         }
  364.     return(0);
  365. }
  366.  
  367. void dopwd
  368.   (
  369.     char *where,
  370.     int howmuch
  371.   )
  372. {
  373.     char *temp;
  374.     int i;
  375.  
  376.     if (howmuch<255) return/*(-1)*/;
  377.     setvol(0L, defaultv);                            /* Go to default Directory */
  378.     temp = (char *) getWDname();
  379.     where[0]='/';
  380.     for( i=1;i<=temp[0];i++) where[i]=temp[i];
  381.     where[temp[0]]=0;
  382.     putln(&temp[1]);
  383.     DisposPtr(temp);
  384.     putln(where);
  385. }
  386.  
  387.  
  388. int chgdir
  389.   (
  390.     char *modifier
  391.   )
  392. {
  393.     WDPBRec pb;
  394.     char tempst[256], *nSlash, *start;
  395.     int i=0, j=0;
  396.  
  397.     if (! isHFS()) return(-1);
  398.  
  399.     setvol(0L, defaultv);                            /* Go to default Directory */
  400.  
  401.     start = (char *) getWDname();
  402.     DisposPtr(start);
  403.  
  404.     start=modifier;
  405.     tempst[0]=':';
  406.  
  407.     putln(start);
  408.  
  409.     if ( strcmp("..",modifier)==0) {
  410.         modifier[0]=0;
  411.         j++;
  412.         }
  413.     else
  414.     while ( (nSlash = index(modifier,'/') ) !=0L) {
  415.         if (( (nSlash -modifier) ==2) && (*modifier=='.') && (*(modifier+1) =='.') ) {
  416.             modifier +=3L;
  417.             start=modifier;
  418.             j++;
  419.             }
  420.         else {
  421.             *nSlash=':';
  422.             modifier=nSlash+1L;
  423.             }
  424.         }
  425.  
  426.     if ( *start==':') {
  427.         strcpy( tempst, start+1L);
  428.         j= strlen( tempst);
  429.         if (tempst[j-1] !=':') {
  430.             tempst[j] = ':';
  431.             tempst[j+1]='\000';
  432.             }
  433.         }
  434.     else
  435.         strcpy( tempst+1L, start);
  436.  
  437.     putln(tempst);
  438.  
  439.     pb.ioNamePtr=ctop(tempst);
  440. #ifdef MPW
  441.     c2pstr(tempst);
  442. #endif
  443.     pb.ioVRefNum=0;
  444.     pb.ioWDDirID=DirTree[j];
  445.     pb.ioCompletion=0L;
  446.     pb.ioWDProcID='NCSA';
  447.  
  448.     j = PBOpenWD( &pb,0);
  449.  
  450.     if ( (j==-35) || (j==-43) ) return(1);
  451.  
  452. #ifdef OLDM
  453.     setvol(0L, (defaultv=getWDnum()) );
  454. #endif
  455.  
  456.     j = setvol(0L, (defaultv=pb.ioVRefNum));
  457.  
  458.     if ( (j==-35) || (j==-43) ) return(1);
  459.  
  460.     return(0);
  461. }
  462.  
  463. /*
  464.  *
  465.  */
  466.  
  467. int Scolorset
  468.   (
  469.     unsigned int *ip,
  470.     char *s
  471.   )
  472. {
  473.     if (3==sscanf(s,"{%d,%d,%d}", &ip[0], &ip[1],&ip[2]) )
  474.         return(1);
  475.     return(0);
  476. }
  477.  
  478. long time(x)
  479. long x;
  480. {
  481. #pragma unused(x)
  482.     return( TickCount()/60);
  483. }
  484.  
  485. int memcmp( a, b, len)
  486. char *a, *b;
  487. int len;
  488. {
  489.     while (len --)
  490.         if (*a!=*b) 
  491.             return(1);
  492.     return(0);
  493. }
  494.  
  495. /**************************************************************************/
  496. /*  defdir and sysdir
  497. *   Use to change to and from the system folder directory.
  498. */
  499. #define CurrentVersion 1            /* Last known SysEnvirons version */
  500.  
  501. void sysdir
  502.   (
  503.     void
  504.   )
  505. {
  506.     int err;
  507.     char s[50];
  508.     SysEnvRec theWorld;                /* System Environment record */
  509.     
  510.     err = SysEnvirons(CurrentVersion, &theWorld);
  511.     if (err == envVersTooBig) {
  512.         /* should post a message saying we need to be updated */
  513.         putln("SysEnvirons out of date - macutil.c");
  514.     }
  515.     if (err != noErr)                /* possibly not available assume nothing here! */
  516.         return;
  517.  
  518.     (void) setvol(NULL, theWorld.sysVRefNum);    /* change default volume */
  519.     sprintf(s,"set sysdir: %d",theWorld.sysVRefNum);
  520.     putln(s);
  521.     
  522. }
  523.  
  524. int setmydir
  525.   (
  526.     void
  527.   )
  528. {
  529.     char s[50];
  530.     
  531.     (void) setvol(NULL, defaultv);
  532.     sprintf(s,"set defdir: %d",defaultv);
  533.     putln(s);
  534.     return(defaultv);
  535. }
  536.  
  537. int makemydir
  538.   (
  539.     int dir
  540.   )
  541. {
  542.     char s[50];
  543.     
  544.     defaultv = dir;
  545.     setvol(NULL, defaultv);
  546.     sprintf(s,"make dir: %d",defaultv);
  547.     putln(s);
  548.     return(defaultv);
  549.     
  550. }
  551.